package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbCertificate;
import com.shenmazong.servereduresume.mapper.TbCertificateMapper;
import com.shenmazong.servereduresume.service.TbCertificateService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbCertificateServiceImpl extends ServiceImpl<TbCertificateMapper, TbCertificate>
implements TbCertificateService {

    @Autowired
    private TbCertificateMapper tbCertificateMapper;

    @Override
    public ResultResponse findAllCertificate(ListParamVo listParamVo) {
        IPage<TbCertificate> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());
        IPage<TbCertificate> certificateIPage = tbCertificateMapper.selectPage(page, new QueryWrapper<TbCertificate>().lambda().eq(TbCertificate::getDeleteFlag, 0));

        return ResultResponse.SUCCESS(certificateIPage);
    }

    @Override
    public ResultResponse saveCertificate(TbCertificate tbCertificate) {
        try {
            tbCertificateMapper.insert(tbCertificate);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED("添加失败");
        }
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse updateCertificate(TbCertificate tbCertificate) {
        try {
            tbCertificateMapper.updateById(tbCertificate);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse deleteCertificate(BatchChooseIdVo batchChooseIdVo) {

        if (batchChooseIdVo.getIds().size() <= 0){
            return ResultResponse.FAILED("至少选择一个删除");
        }

        try {
            for (Long id : batchChooseIdVo.getIds()) {
                UpdateWrapper<TbCertificate> certificate = new UpdateWrapper<TbCertificate>().eq("certificate_id", id);
                certificate.set("delete_flag",1);
                tbCertificateMapper.delete(certificate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS("删除成功");
    }
}




